package pswvclass.pswvandroid2.com.user;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.Login;
import pswvclass.pswvandroid2.com.util.DBHelper;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 * Created by Administrator on 2018/12/18.
 */
public class UserDatumEdit extends Fragment implements View.OnClickListener {

    private String tag = "UserDatumEdit";
    private TextView user_name1;
    private TextView user_name;
    private TextView user_sex;
    private TextView user_degree;
    private TextView user_interest;
    private TextView user_strong_point;
    private View user_save;
    protected static final int SAVE_SUCCESS = 2;
    protected static final int SUCCESS = 1;
    protected static final int ERROR = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.pswv_more_user_datum_edit, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        user_name1= (TextView) getView().findViewById(R.id.user_name1);
        user_name= (TextView) getView().findViewById(R.id.user_name);
        user_sex= (TextView) getView().findViewById(R.id.user_sex);
        user_degree= (TextView) getView().findViewById(R.id.user_degree);
        user_interest= (TextView) getView().findViewById(R.id.user_interest);
        user_strong_point= (TextView) getView().findViewById(R.id.user_strong_point);
        user_save = getActivity().findViewById(R.id.user_save);
        user_save.setOnClickListener(this);
        DBHelper db = new DBHelper(getContext());
        SQLiteDatabase sd = db.getWritableDatabase();
        int userId = 0;
        Cursor cursor = sd.query("user", new String[]{"userId"}, "state=?", new String[]{"1"}, null, null, null);
        while (cursor.moveToNext()) {
            userId = cursor.getInt(0);
        }
        cursor.close();
        final int finalUserId = userId;
        new Thread() {
            public void run() {
                try {
                    HttpUtils hu = new HttpUtils();
                    String data2;
                    final String src = getResources().getString(R.string.src_mysql) + "pswv/api/userAction/queryUserById";
                    final Map<String, String> map1 = new HashMap<String, String>();
                    map1.put("userId", finalUserId + "");
                    data2 = hu.submitPostData(src, map1, "utf-8");
                    JSONObject jsonObj = new JSONObject(data2);
                    JSONObject objectt = jsonObj.getJSONObject("response");
                    int result = Integer.parseInt(jsonObj.getString("code"));
                    if (result == 0) {
                        Message msg = Message.obtain();
                        msg.obj=objectt;
                        msg.what = SUCCESS;
                        mHandler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = Message.obtain();
                    msg.what = ERROR;
                    mHandler.sendMessage(msg);
                    Log.v(tag, e + "");
                }
            }
        }.start();
    }


    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case SAVE_SUCCESS:
                    Toast.makeText(getContext(), " 修改成功", Toast.LENGTH_SHORT).show();
                    break;
                case SUCCESS:
                JSONObject jsonObj= (JSONObject) msg.obj;
                try {
                        user_name.setText(jsonObj.getString("name"));
                        user_name1.setText(jsonObj.getString("name"));
                        user_sex.setText(jsonObj.getString("sex"));
                        user_degree.setText(jsonObj.getString("degree"));
                        user_interest.setText(jsonObj.getString("interest"));
                        user_strong_point.setText(jsonObj.getString("strongPoint"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                break;
                case ERROR:
                    break;
                default:
                    break;
            }

        }

        ;

    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_save:
                DBHelper db = new DBHelper(getContext());
                SQLiteDatabase sd = db.getWritableDatabase();
                int userId = 0;
                Cursor cursor = sd.query("user", new String[]{"userId"}, "state=?", new String[]{"1"}, null, null, null);
                while (cursor.moveToNext()) {
                    userId = cursor.getInt(0);
                }
                cursor.close();
                final int finalUserId = userId;
                new Thread() {
                    public void run() {
                        try {
                            HttpUtils hu = new HttpUtils();
                            String data2;
                            final String src = getResources().getString(R.string.src_mysql) + "pswv/api/userAction/editUserDatum";
                            final Map<String, String> map1 = new HashMap<String, String>();
                            map1.put("userId", finalUserId + "");
                            map1.put("name", user_name1.getText().toString().trim());
                            map1.put("sex", user_sex.getText().toString().trim());
                            map1.put("degree", user_degree.getText().toString().trim());
                            map1.put("interest", user_interest.getText().toString().trim());
                            map1.put("strongpoint", user_strong_point.getText().toString().trim());
                            Log.v(user_degree.getText().toString().trim(),"");
                            data2 = hu.submitPostData(src, map1, "utf-8");
                            JSONObject jsonObj = new JSONObject(data2);
                            int result = Integer.parseInt(jsonObj.getString("code"));
                            if (result == 0) {
                                Message msg = Message.obtain();
                                msg.what = SAVE_SUCCESS;
                                mHandler.sendMessage(msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Message msg = Message.obtain();
                            msg.what = ERROR;
                            mHandler.sendMessage(msg);
                            Log.v(tag, e + "");
                        }
                    }
                }.start();
                break;
            default:
                break;
        }
    }
}
